package com.y.fund.web.mem;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.y.fund.aacore.data.DataVo;
import com.y.fund.aacore.enums.BoolVal;
import com.y.fund.aacore.vo.PagingVo;
import com.y.fund.domain.com.CO12Message;
import com.y.fund.handler.CurrLgnUserVo;
import com.y.fund.service.mem.UserService;
import com.y.fund.utils.IdGenerator;
import com.y.fund.utils.SQLUtil;
import com.y.fund.web.BaseController;
import com.y.fund.web.aavo.com.LgnUserVo;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@ApiSort(71)
@RestController
@RequestMapping(value="api/msg", produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(name= "消息管理")
public class MessageController extends BaseController{

	@Autowired
	protected UserService userService;
	
    @GetMapping(value = "list")
	@Operation(summary = "分页查询消息列表")
    @ApiOperationSupport(order = 1)
    public DataVo userList(CO12Message msg, PagingVo pvo) {
    	return DataVo.Success(commService.findPagingByExpLikeOrderCountById(msg, pvo, SQLUtil.EditTimeDescOrder));
    }

	@PostMapping(value = "sv")
	@Operation(summary = "保存消息记录")
    public DataVo save(@RequestBody CO12Message[] msgs) throws Exception{
		LgnUserVo luv = CurrLgnUserVo.Get();
		List addObjs = new ArrayList();
		Set<String> ids = new HashSet<String>();
		for(CO12Message msg : msgs) {
			msg.setId(IdGenerator.Id32());
			msg.setRead(BoolVal.No.getVal());
			addObjs.add(msg);
			ids.add(msg.getId());
		}
		
		if(commService.addObjs(addObjs)) {
			return DataVo.Success(ids);
		}else {
			return DataVo.FailDB();
		}
	}

	@GetMapping(value = "detail")
	@Operation(summary = "根据ID查看消息详情")
	@ApiOperationSupport(order = 13)
	public DataVo detail(@RequestParam(name="id", required = true) final String msgId) {
		CO12Message msg = commService.findObjById(CO12Message.class, msgId);
		return DataVo.Success(msg);
	}

}